package com.poisonh.unifiedauth.widget.pinview;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;


import com.poisonh.unifiedauth.R;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 虚拟键盘
 */
public class KeyboardView extends RelativeLayout
{

    private Context context;

    private GridView gridView;

    private ArrayList<Map<String, String>> valueList;
    private EditText textAmount;
    private int dosLength;

    public KeyboardView(Context context)
    {
        this(context, null);
    }

    public KeyboardView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        this.context = context;

        View view = View.inflate(context, R.layout.layout_keyboard, null);

        initView(view);
        valueList = new ArrayList<>();


        gridView = (GridView) view.findViewById(R.id.gv_keybord);

        initValueList();

        setupView();
        addView(view);
        gridView.setOnItemClickListener(onItemClickListener);
    }

    private void initView(View view)
    {
        textAmount = (EditText) view.findViewById(R.id.textAmount);
        // 设置不调用系统键盘
        if (android.os.Build.VERSION.SDK_INT <= 10)
        {
            textAmount.setInputType(InputType.TYPE_NULL);
        } else
        {
            try
            {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(textAmount, false);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }


    public ArrayList<Map<String, String>> getValueList()
    {
        return valueList;
    }

    private void initValueList()
    {

        // 初始化按钮上应该显示的数字
        for (int i = 1; i < 13; i++)
        {
            Map<String, String> map = new HashMap<>();
            if (i < 10)
            {
                map.put("name", String.valueOf(i));
            } else if (i == 10)
            {
                map.put("name", "");
            } else if (i == 11)
            {
                map.put("name", String.valueOf(0));
            } else if (i == 12)
            {
                map.put("name", "");
            }
            valueList.add(map);
        }
    }

    public GridView getGridView()
    {
        return gridView;
    }

    private void setupView()
    {

        KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(context, valueList);
        gridView.setAdapter(keyBoardAdapter);
    }


    public void setFinish(final OnPasswordInputFinish pass)
    {
        textAmount.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // pass.inputFinish(s.toString());
            }


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                pass.inputChange(s.length());
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if (s.length() == 6)
                {
                    pass.inputFinish(s.toString());
                }
            }
        });
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
        {
            if (position < 11 && position != 9)
            {    //点击0~9按钮

                String amount = textAmount.getText().toString().trim();
                amount = amount + valueList.get(position).get("name");

                textAmount.setText(amount);

                Editable ea = textAmount.getText();
                setDosLength(ea.length());
                textAmount.setSelection(ea.length());
            } else if (position == 11)
            {     //点击退格键
                String amount = textAmount.getText().toString().trim();
                if (amount.length() > 0)
                {
                    amount = amount.substring(0, amount.length() - 1);
                    textAmount.setText(amount);

                    Editable ea = textAmount.getText();
                    setDosLength(ea.length());
                    textAmount.setSelection(ea.length());
                }
            }
        }
    };


    /**
     * 清空长度
     */
    public void cleanLength()
    {
        textAmount.setText("");
    }

    public void setDosLength(int dosLength)
    {
        this.dosLength = dosLength;
    }

}
